
*******CREATING BASELINE DATA FOR TABLE 1 & ONLINE APPENDIX D***********

set more off
clear
clear matrix
capture log close



//--> BASELINE DATA FROM FA SURVEY 
	*********************************************************************************************************************************************************
		
		use "$data/FASVY.dta", clear 
		
		//--> CONSTRUCT DUMMy VARIABLE INDICATING RESPONSE "I FEEL LIKE I CAN REALLY HELP PEOPLE"
		
			g job_like_help = name_thing_likemost_nrsp==4 
			
		local svy_var age female marital_status head_hh education consumption_hh work_month nfirst_job work_vb  still_work_nrsp_2years overtime_perday credit_bonus soc_help_credit volunteer_before_nrsp job_like_help 
		keep fw_code `svy_var'
		tempfile temp_svy
		save `temp_svy' 
		
		
			
//--> PRETREATMENT MIS DATA 
	*********************************************************************************************************************************************************

		//--> FWCO PANEL: Maps each FAs CO portfolio
		
				use "$data/FWCO_panel.dta"
				
		//--> MERGE w/ CO Month Level MIS data		
				
				sort co_code month 
				merge co_code month using "$data/MIS_comthlevel.dta"
				drop _m
				
		//--> KEEP ONLY PREBONUS MONTHS (1-9 months); month 10 is bonus annoucement month also dropped
					
				keep if month<10
				
		//--> SOME COS are COMANAGED (SCALE THE ABSOLUTE NUMBERS BY COSHARE WEIGHT)	
			
			
				g weighted_amount_issue = coshare_weight*amount_issue
				g weighted_num_active_loan = coshare_weight*num_active_loan
				g weighted_num_new_loan = coshare_weight*num_new_loan
				g wmiss_pay_maturity_month = coshare_weight*miss_pay_maturity_month
				g weighted_num_co_alive = coshare_weight*alive_co

				
				
		//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
			*****************************************

				local var weighted_num_co_alive weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month miss_pay_month miss_pay_20 num_pay
				collapse (sum) `var', by(fw_code month)
				
				//--> CALCULATE REPAYMENT RATES (RATIOS)
				
						g inv_ratio_miss_pay_month = 1 - (miss_pay_month/num_pay)
						g inv_ratio_miss_pay_20 = 1 - (miss_pay_20/num_pay)
				
		//--> CALCULATE AVERAGE PERFORMANCE (FA LEVEL)
			*****************************************

				local var weighted_num_co_alive weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month inv_ratio_miss_pay_month inv_ratio_miss_pay_20
				collapse (mean) `var', by(fw_code)
				
					
					
		//--> CALCULATE CREDIT INDEX
			*****************************************					
					
				local credit_var weighted_num_active_loan weighted_num_new_loan weighted_amount_issue wmiss_pay_maturity_month inv_ratio_miss_pay_month inv_ratio_miss_pay_20	
									
				//--> STANDARDIZE THE SIX CREDIT OUTCOMES
					
						foreach m in `credit_var' {
							egen std`m' = std(`m')
							}

					//--> INVERSE THE SCALE OF NUMBER OF LOANS PAST MATURITY
						
							foreach x in wmiss_pay_maturity_month {
								replace std`x' = -1*std`x'
								}
								
				egen credit_index = rowmean(stdweighted_num_active_loan stdinv_ratio_miss_pay_20 stdweighted_num_new_loan stdweighted_amount_issue stdinv_ratio_miss_pay_month stdwmiss_pay_maturity_month)
				replace credit_index =. if (stdweighted_num_active_loan==.| stdinv_ratio_miss_pay_20==.| stdweighted_num_new_loan==.| stdweighted_amount_issue==.| stdinv_ratio_miss_pay_month==.|stdwmiss_pay_maturity_month==.)
				egen stdcredit_index= std(credit_index)

				sum `credit_var' stdcredit_index

				//--> ASSIGN MISSING PRETREATMENT VARS TO ZERO AND CREATE MISSING DUMMY; 2 RATIO VARS WHERE NO LOANS WERE DUE
								
						foreach v in `credit_var' stdcredit_index {
							g m`v' = (`v'==.)
							replace `v' = 0 if `v' ==.
							}
				
		sort fw_code
		tempfile temp_mis
		save `temp_mis'
		
				
//--> COMBINED DATA		
	*********************************************************************************************************************************************************

		
		use `temp_svy', clear
		sort fw_code 
		merge fw_code using `temp_mis'
		drop _m

		sort fw_code
		save "$data/baseline_fa_vars.dta", replace
	
	
				
				

				
				




	//--> 


	    /*--> OUTPUT: SUM STAT XLS ......ADD MORE VARS ON INFORMATION BIAS FOR TABLE 1 and FIG 1
			local var age country_philippines country_indonesia married year_abroad year_singapore year_employer salary workhours offdays highfreedom law_transfer stddiff_med_index stddiff_rel_index
			
			local row  = 1
			foreach v in `var' {
			
			sum `v', d
			local m = r(mean)
			local m2 = r(sd)
			local m3 = r(p50)
			local m4 = r(p25)
			local m5 = r(p75)
			
			matrix sumstats2_out[`row',1]=r(mean)
			matrix sumstats2_out[`row',2]=r(sd)
			matrix sumstats2_out[`row',3]=r(p50)
			matrix sumstats2_out[`row',4]=r(p25)
			matrix sumstats2_out[`row',5]=r(p75)

			local row = `row' + 1
			}

			drop _all
			svmat sumstats2_out
			outsheet using "$DIR2/sumstat2.xls", replace
	    */			
